package com.wc.acwing.基础算法.双指针.双端队列;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/10 9:35
 * @description 双端队列
 * https://www.acwing.com/problem/content/5484/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static int[] a = new int[N];
    static int n;

    // 分情况 a != b  a > b, a < b 能选小的就选小的
    // a == b 就只能从一边走了, 看选哪边走的更长，就走哪边
    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        int l = 1, r = n;
        int pre = 0;
        while (l <= r) {
            // 判断哪边小
            if (a[l] < a[r]) {
                if (a[l] > pre) {
                    pre = a[l];
                    l++;
                } else if (a[r] > pre) {
                    pre = a[r];
                    r--;
                } else {
                    break;
                }
            } else if (a[l] > a[r]) {
                if (a[r] > pre) {
                    pre = a[r];
                    r--;
                } else if (a[l] > pre) {
                    pre = a[l];
                    l++;
                } else {
                    break;
                }
            } else if (a[l] <= pre) {
                break;
                // 相等判断哪边长
            } else {
                // 相等之后，只能完全选一边了,我草，很变态
                int nextL = l + 1;
                while (nextL < r && a[nextL] > a[nextL - 1]) {
                    nextL++;
                }
                int nextR = r - 1;
                while (nextR > l && a[nextR] > a[nextR + 1]) {
                    nextR--;
                }
                if (r - nextR > nextL - l) {
                    r = nextR;
                } else {
                    l = nextL;
                }
                break;
            }
        }
        int res = l - 1 + n - r;
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
